package com.achao.view;

import com.achao.javabean.vo.TrainVo;
import com.achao.service.impl.TrainServiceImpl;
import com.achao.utils.ParamUtils;
import com.achao.utils.StringUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * 首页
 *
 * @Author: achao
 * @DateTime: 2022/7/26 12:18
 * @Description: TODO
 */
public class UserIndexView extends JFrame {
    private static UserIndexView instance;

    public static UserIndexView getInstance() {
        if (instance == null)
            instance = new UserIndexView();
        return instance;
    }

    private JLabel startSiteLabel = new JLabel("出发点: ");
    private JLabel endSiteLabel = new JLabel("目的地: ");
    private JTextField startSiteText = new JTextField();
    private JTextField endSiteText = new JTextField();
    private JButton findBtn = new JButton("查询");

    private DefaultTableModel model;
    private JTable table;

    public UserIndexView() {
        setSize(500, 600);
        setLayout(null);
        setResizable(false);
        setDefaultCloseOperation(0);
        setLocationRelativeTo(null);

        init();
        action();

    }

    private void init() {

        add(startSiteLabel);
        startSiteLabel.setBounds(20, 10, 80, 30);

        add(startSiteText);
        startSiteText.setBounds(80, 10, 100, 30);

        add(endSiteLabel);
        endSiteLabel.setBounds(210, 10, 80, 30);

        add(endSiteText);
        endSiteText.setBounds(270, 10, 100, 30);

        add(findBtn);
        findBtn.setBounds(390, 10, 100, 30);

        model = new DefaultTableModel() {
            // 将表格设为不可编缉状态
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        table = new JTable(model);
        table.setOpaque(false);

        // 设置表头
        model.setColumnIdentifiers(ParamUtils.INIT_COL_NAME);
        JScrollPane pane = new JScrollPane();
        pane.setViewportView(table);
        add(pane);
        pane.setBounds(20, 50, 450, 500);

    }

    private void action() {
        findBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                find();
            }
        });
    }

    private void find() {
        System.out.println("点击了搜索");
        model.setRowCount(0);
        if (StringUtils.isNotEmpty(startSiteText.getText()) && StringUtils.isNotEmpty(endSiteText.getText())) {
            TrainServiceImpl trainService = new TrainServiceImpl();
            List<TrainVo> list = trainService.showInf(startSiteText.getText(), endSiteText.getText());
            System.out.println(startSiteText.getText()+"|"+ endSiteText.getText());
            for (TrainVo trainVo3 : list) {
                Object[] data = {trainVo3.getTrainName(), trainVo3.getStartSite(),
                trainVo3.getEndSite(), trainVo3.getStartTime(), trainVo3.getEndTime()};
                model.addRow(data);
            }
        }

    }
}
